﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Microsoft.Win32;

namespace RG.Common
{
    public static class RegistryKeyConsts
    {
        public const string Fx10 = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.0";
        public const string Fx11 = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322";
        public const string Fx20 = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727";
        public const string Fx30 = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup";
        public const string Fx35 = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5";
        public const string Fx4Client = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client";
        public const string Fx4Full = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full";

        private static readonly List<string> AllVersions = new List<string>
                                                      {
                                                          Fx4Full,
                                                          Fx4Client,
                                                          Fx35,
                                                          Fx30,
                                                          Fx20,
                                                          Fx11,
                                                          Fx10
                                                      };

        const string ni = "NotInstalled";
        
        public static string GetFrameWorkVersion(string key)
        {
            using (RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, String.Empty))
            {
                using (RegistryKey registryKey = baseKey.OpenSubKey(key, RegistryKeyPermissionCheck.Default))
                {
                    if (registryKey != null)
                    {
                        object regValue = registryKey.GetValue("Version", null);
                        object sp_value = registryKey.GetValue("SP", null);
                        if (regValue != null && sp_value == null)
                            return (string)Convert.ChangeType(regValue, typeof(string), CultureInfo.InvariantCulture);
                        if (regValue != null)
                            return string.Format("{0} SP{1}", Convert.ChangeType(regValue, typeof(string), CultureInfo.InvariantCulture), Convert.ChangeType(sp_value, typeof(string), CultureInfo.InvariantCulture));
                    }
                }
            }
            return ni;
        }

        public static string GetMaxFrameWorkVersion()
        {
            foreach (var version in AllVersions)
            {
                string sfx = "";
                if (version == Fx4Full) sfx = " Full";
                if (version == Fx4Client) sfx = " Client";
                var key = GetFrameWorkVersion(version);
                if (key != ni) return key + sfx;
            }
            return "";
        }

        public static string GetAllFrameWorkVersions()
        {
            var sb = new StringBuilder();
            foreach (var version in AllVersions)
            {
                string sfx = "";
                if (version == Fx4Full) sfx = " Full";
                if (version == Fx4Client) sfx = " Client";
                var key = GetFrameWorkVersion(version);
                if (key != ni) sb.Append(key + sfx + ", ");
            }
            return sb.ToString();
        }
    }
}
